package org.flint92.springcloud.feign.provider.controller;

import org.flint92.user.domain.User;
import org.flint92.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author flint92
 */
@RestController
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/user/save")
    public boolean save(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @GetMapping("/user/list")
    public List<User> findAll() {
        long executeTime = ThreadLocalRandom.current().nextInt(1000);
        if (logger.isInfoEnabled()) {
            logger.info("超时时间: " + executeTime + "ms.");
        }

        // 模拟超时
        try {
            TimeUnit.MILLISECONDS.sleep(executeTime);
        } catch (InterruptedException ignored) {
            //
        }

        return userService.findAll();
    }

}
